﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.ComponentModel;
using UmlControlLibrary;

namespace UmlControlLibrary
{
    public class UmlStruct : DiagramElement
    {
        public UmlStruct(string structName)
            : base(structName)
        {
            MemberList = new ObservableCollection<UmlProperty>();
            MemberList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(MemberList_CollectionChanged);
            Metadata.Add("AllowUmlTreeviewDnd", true);
            this.Stereotype = "Struct";
        }


        public ObservableCollection<UmlProperty> MemberList
        {
            get;
            set;
        }

        public List<Object> MemberListView
        {
            get
            {
                if ((this.Metadata.Get<bool>("DeferredLoading")) && (!this.Metadata.Get<bool>("DeferredLoadingComplete")))
                {
//                    DeferredLoadProperties();
                }
                return PropertyGroupingHelper.Group(MemberList.Cast<UmlMemberElement>());
            }
        }

        void MemberList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            foreach (UmlProperty p in (sender as IEnumerable<UmlProperty>))
            {
                p.PropertyChanged -= new PropertyChangedEventHandler(MemberListChanged);
                p.PropertyChanged += new PropertyChangedEventHandler(MemberListChanged);
            }
            OnPropertyChanged("MemberListView");
            OnPropertyChanged("Childs");
        }

        void MemberListChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged("MemberListView");
            OnPropertyChanged("Childs");
        }

        protected override ControlTemplate GetTemplate()
        {
            return DiagramElement.DiagramElementTemplateFactory(typeof(UmlStructControl));
        }

        public override ObservableCollection<UmlElement> GetChilds()
        {
            ObservableCollection<UmlElement> elements = base.GetChilds();
            foreach (UmlProperty property in MemberList)
                elements.Add(property);
            return elements;
        }
    }
}
